#coding=utf8
import sys, MangaBase, re, urllib2, urlparse
import cmrhelper
from cmrhelper import GbkToUtf8, UtfToGbk

ServerList = ['']*20
class Parser(MangaBase.CMangaBase):
    "zxmh.net parser"
    def Start(self):
        self.IniRe()
        try:
            self.chaptercontent = urllib2.urlopen(self.m_url).read()
        except Exception, e:
            self.m_mangalog.writeLog('get page content fail: %s ' % (str(e)),'fatal')
            return
        # if m_manganame is None,then parse manganame
        if not self.m_manganame:
            self.ParseName()
        self.GetChapters()
        if self.m_chapterlist:
            for item in self.m_chapterlist:
                self.ParseChapter(item)
        self.End()

    def IniRe(self):
        self.namere = re.compile("<strong>(.*?)免费在线漫画列表</strong>")
        self.chapterre = re.compile(r'cococomic.htm\?a=(\d+?)&c=(\d+?)&p=(\d+?)&s=(\d+?) target=_blank><img src=/images/c.gif border=0 hspace=5>(.*?)</a>')
        self.picre = re.compile("<PicUrl Value=\"(.*?)\">")
        self.cptnamere = re.compile("ComicListName=\"(.*?)\"")
        self.xmlurl = "http://dm3.cococomic.com/"
        
        # urly code...
        self.ServerList = {}
        self.ServerList[1] = "http://76.99770.com/img01/"
        self.ServerList[2] = "http://16.99770.com:8002/"
        self.ServerList[3] = "http://76.99770.com/img03/"
        self.ServerList[4] = "http://39.99770.com:8004/"
        self.ServerList[5] = "http://76.99770.com/img05/"
        self.ServerList[6] = "http://16.99770.com:8006/"
        self.ServerList[7] = "http://16.99770.com:8007/"
        self.ServerList[8] = "http://232.99770.com:8008/"
        self.ServerList[9] = "http://232.99770.com:8009/"
        self.ServerList[10] = "http://232.99770.com:8010/"
        self.otherServer = "http://other.99770.com"

    def ParseName(self):
        "get manga name if name is None"
        self.m_mangalog.writeLog('get manga name')
        self.m_manganame = 'noname'

    def ParseChapter(self, cpt):
        "parse each chapter to generate url of pictures"
        try:
            turl = urlparse.urljoin(self.xmlurl, "/cocoxml/" + cpt[0] + "/" + cpt[1] + ".xml")
            self.m_mangalog.writeLog('Download chapter xml: %s' % turl.decode('gbk', 'ignore'), 'info')
            content = urllib2.urlopen(turl).read()
            tname = self.cptnamere.findall(content)[0]
            if content and self.picre.findall(content):
                for item in self.picre.findall(content):
                    purl = self.ServerList[int(cpt[3])] + item
                    self.PushPic([purl, purl, tname, self.m_manganame])
        except  Exception, e:
            self.m_mangalog.writeLog(str(e) + ' in ParseChapter', 'error')
    
    def GetChapters(self):
        "get manga chapters"
        try:
            if self.chapterre.findall(self.chaptercontent):
                for item in self.chapterre.findall(self.chaptercontent):
                    self.m_chapterlist.append(item)
                    self.m_chaptercnt += 1
            self.m_mangalog.writeLog('get chapters finish total: [%d] chapters'\
                                         % self.m_chaptercnt)
        except  Exception, e:
            self.m_mangalog.writeLog(str(e)+' in getchapter','error')
